package com.penghe.www.httplibrary

import android.content.Context
import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley


/**
 * Created by pengh on 2017/5/29.
 *
 * volley方式请求的实体类
 */

class VolleyProcessor : IHttpProcessor {

    private var mQueue: RequestQueue? = null
    private var TAG: String = "VolleyProcessor"

    @Suppress("ConvertSecondaryConstructorToPrimary")
    constructor(context: Context) {
        mQueue = Volley.newRequestQueue(context)
    }

    override fun get(url: String, params: Map<String, Any>, callBack: ICallBack) {
        val stringRequest: StringRequest = StringRequest(Request.Method.GET, url, Response.Listener<String> {
            response ->
            Tool.w(TAG, "response")
            callBack.onSucceeded(response)
        }, Response.ErrorListener {
            error ->
            Tool.w(TAG, "error")
            callBack.onFailed(error.toString())
        })

        mQueue!!.add(stringRequest)
    }

    override fun post(url: String, params: Map<String, Any>, callBack: ICallBack) {
        val stringRequest: StringRequest = StringRequest(Request.Method.POST, url, Response.Listener<String> {
            response ->
            Tool.w(TAG, "response")
            callBack.onSucceeded(response)
        }, Response.ErrorListener {
            error ->
            Tool.w(TAG, "error")
            callBack.onFailed(error.toString())
        })
        mQueue!!.add(stringRequest)
    }

}